﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Data.Infrastructure;
using Data.Repositories;
using Domain;
using Domain.Entities;
using Domain.Specification;

namespace Service
{
    public class ProductsService<T, TPageData> :IProductsService<Product,PageData<Product>>, IService<Product, PageData<Product>>
    {
        private readonly IProductsRepository productRepository;
        private readonly IUnitOfWork unitOfWork;
        public ProductsService(IProductsRepository productRepository, IUnitOfWork unitOfWork)
        {
            this.productRepository = productRepository;
            this.unitOfWork = unitOfWork;
        }
        public void Commit()
        {
            unitOfWork.Commit();
        }

        public void Add(Product entity)
        {
            productRepository.Add(entity);
            unitOfWork.Commit();
        }

        public void Update(Product entity)
        {
            productRepository.Update(entity);
            unitOfWork.Commit();
        }

        public void Delete(Product entity)
       {
           productRepository.Delete(entity);
           unitOfWork.Commit();
       }

        public void Delete(Func<Product, Boolean> predicate)
        {
            productRepository.Delete(predicate);
            //unitOfWork.Commit();
        }

        public Product GetById(int id)
        {
            return productRepository.GetById(id);
        }

        public Product Get(Func<Product, Boolean> where)
        {
            return productRepository.Get(where);
        }

        public IQueryable<Product> GetAll()
        {
            return productRepository.GetAll();
        }

        public IEnumerable<Product> GetMany(Func<Product, bool> where)
        {
            return productRepository.GetMany(where);
        }

        public PageData<Product> Get<TS>(int pageIndex, int pageSize, Expression<Func<Product, TS>> orderByExpression, bool isDesc)
        {
            return productRepository.Get(pageIndex, pageSize, orderByExpression, isDesc);
        }

        public PageData<Product> Get<TS>(int pageIndex, int pageSize, ISpecification<Product> condition,
                                Expression<Func<Product, TS>> orderByExpression, bool isDesc)
        {
            return productRepository.Get(pageIndex, pageSize, condition, orderByExpression, isDesc);

        }

        public List<Product> GetListByTopN<TS>(int topN, ISpecification<Product> condition, Expression<Func<Product, TS>> orderByExpression,
                                        bool isDesc)
        {
            return productRepository.GetListByTopN(topN, condition, orderByExpression, isDesc);
        }
    }
}
